﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace BAL
{
    public partial class Abandoned : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        public string key = "";
        public string FBillNo = "";
        public string Fry = "";
        public int Num = 0;
        public string FSku;
        public string Fdate;
        public string StickLen;
        private string ID;

        
        public Abandoned()
        {
            InitializeComponent();
        }

        public Abandoned(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
        }

        private void Abandoned_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }

        private void BinData()
        { 
            DataSet ds = bjs.GetAbandonList();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }

        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "Abandoned";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "Num";
            cl1.Width = 20;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "出库单号";
            cl2.MappingName = "FBillNo";
            cl2.Width = 110;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "开单时间";
            cl3.MappingName = "Fdate";
            cl3.Width = 70;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "开单人";
            cl4.MappingName = "NAME";
            cl4.Width = 50;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "总数";
            cl5.MappingName = "Total";
            cl5.Width = 30;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "未扫";
            cl6.MappingName = "LeftQty";
            cl6.Width = 30;
            dts.GridColumnStyles.Add(cl6);

                      
            dataGrid1.TableStyles.Add(dts);
        }
        //返回  
        private void button1_Click(object sender, EventArgs e)
        {
            Main2 main = new Main2(this.ID);
            main.Show();
            this.Hide();
        }

        //刷新
        private void button3_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }


        //明细
        private void button4_Click(object sender, EventArgs e)
        {
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("请选择订单", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            AbandonedDetail Adet = new AbandonedDetail(FBillNo, Num, Fry, Fdate,this.ID);
            Adet.Show();
            this.Hide();
        }
        //完成
        private void button2_Click(object sender, EventArgs e)
        {
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("请选择要完成的订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            }
            else
            {
                DialogResult dr = MessageBox.Show("是否完成订单：" + FBillNo + "?", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                {
                    if(dr==DialogResult.Yes)
                    {
                        string txt ="";
                        bool lb = bjs.FinishAbandoned(FBillNo,out txt);
                        if(lb)
                        {
                            DataGridStyle();
                            BinData();
                        }
                        else
                        {
                             MessageBox.Show("完成订单错误！"+txt,"警告！",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button3);
                        }

                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FBillNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
            Fry = ((DataTable)(dataGrid1.DataSource)).Rows[index]["Fry"].ToString();
            Num = Convert.ToInt32(((DataTable)(dataGrid1.DataSource)).Rows[index]["Total"].ToString());
            Fdate = ((DataTable)(dataGrid1.DataSource)).Rows[index]["Fdate"].ToString();
           
        }

    }
}